package com.dmap.knowledge.controller.background;

import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import com.dmap.knowledge.constants.KnowledgeConstant;
import com.dmap.knowledge.controller.api.param.ArticleColumnAppEditParam;
import com.dmap.knowledge.controller.api.param.ArticleColumnAppParam;
import com.dmap.knowledge.service.ArticleColumnAppExtensionService;
import com.dmap.knowledge.service.ArticleColumnAppService;
import com.dmap.knowledge.vo.ArticleColumnCheckVo;
import com.dmap.knowledge.vo.SelectAppColumn;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(value = "App文章专栏", description = "background/app-文章专栏")
@RestController
@RequestMapping(value = "/background/dmap-knowledge-server/v1/column")
public class ArticleColumnAppController {
    @Resource
    private ArticleColumnAppService articleColumnAppService;
    @Resource
    private ArticleColumnAppExtensionService articleColumnAppExtensionService;
    
    @Transactional
    @ResponseBody
    @ApiOperation(value = "新增App文章专栏", notes = "新增App文章专栏")
    @RequestMapping(value = "/app/insert", method = RequestMethod.POST)
    public BaseVo insertColumnApp(@RequestBody ArticleColumnAppParam articleColumnAppParam) {
        BaseVo result = new BaseVo();
        if (articleColumnAppParam.getLang() == null || articleColumnAppParam.getAppNumber() == null || articleColumnAppParam.getAppName() == null || articleColumnAppParam.getArticleColumnId() == null || articleColumnAppParam.getColumnName() == null || articleColumnAppParam.getDescription() == null || articleColumnAppParam.getSort() == 0 || articleColumnAppParam.getStatus() == 0) {
            
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (StringUtils.isBlank(articleColumnAppParam.getLang()) || StringUtils.isBlank(articleColumnAppParam.getAppNumber()) || StringUtils.isBlank(articleColumnAppParam.getAppName()) || !StringUtils.isNumeric(articleColumnAppParam.getArticleColumnId()) || StringUtils.isBlank(articleColumnAppParam.getColumnName()) || StringUtils.isBlank(articleColumnAppParam.getDescription())) {
            
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = articleColumnAppService.insertArticleColumnApp(articleColumnAppParam);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
        }
        if (success == 2) {
            result.setCode(KnowledgeConstant.RESPONSE_APP_COLUMN_MODULE_CODE);
            result.setMsg(KnowledgeConstant.RESPONSE_APP_COLUMN_ERROR_MSG);
        }
        return result;
    }
    
    @Transactional
    @ResponseBody
    @ApiOperation(value = "修改App文章专栏", notes = "修改App文章专栏")
    @RequestMapping(value = "/app/update", method = RequestMethod.POST)
    public BaseVo updateColumnApp(@RequestBody ArticleColumnAppEditParam articleColumnAppEditParam) {
        BaseVo result = new BaseVo();
        if (articleColumnAppEditParam.getColumnAppId() == null || articleColumnAppEditParam.getLang() == null || articleColumnAppEditParam.getAppNumber() == null || articleColumnAppEditParam.getAppName() == null || articleColumnAppEditParam.getArticleColumnId() == null || articleColumnAppEditParam.getColumnName() == null || articleColumnAppEditParam.getDescription() == null || articleColumnAppEditParam.getSort() == 0 || articleColumnAppEditParam.getStatus() == 0) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (!StringUtils.isNumeric(articleColumnAppEditParam.getColumnAppId()) || StringUtils.isBlank(articleColumnAppEditParam.getLang()) || StringUtils.isBlank(articleColumnAppEditParam.getAppNumber()) || StringUtils.isBlank(articleColumnAppEditParam.getAppName()) || !StringUtils.isNumeric(articleColumnAppEditParam.getArticleColumnId()) || StringUtils.isBlank(articleColumnAppEditParam.getColumnName()) || StringUtils.isBlank(articleColumnAppEditParam.getDescription())) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = articleColumnAppService.updateArticleColumnApp(articleColumnAppEditParam);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
        }
        if (success == 2) {
            result.setCode(KnowledgeConstant.RESPONSE_APP_COLUMN_MODULE_CODE);
            result.setMsg(KnowledgeConstant.RESPONSE_APP_COLUMN_ERROR_MSG);
        }
        return result;
    }
    
    @Transactional
    @ResponseBody
    @ApiOperation(value = "删除App文章专栏", notes = "删除App文章专栏")
    @RequestMapping(value = "/app/delete", method = RequestMethod.DELETE)
    public BaseVo deleteColumnApp(@RequestParam String columnAppIds) {
        BaseVo result = new BaseVo();
        if (columnAppIds == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (StringUtils.isBlank(columnAppIds)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = articleColumnAppService.deleteArticleColumnAppList(columnAppIds);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_DELETE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_DELETE_ERROR_MSG);
        }
        return result;
    }
    
    @ResponseBody
    @ApiOperation(value = "条件查询App文章专栏", notes = "条件查询App文章专栏")
    @RequestMapping(value = "app/select", method = RequestMethod.GET)
    public BaseVo selectColumnApp(@RequestParam String lang,
                                  @RequestParam(required = false) String companyNumber,
                                  @RequestParam(required = false) String appNumber,
                                  @RequestParam(required = false) String key,
                                  @RequestParam(required = false, defaultValue = "0") long order,
                                  @RequestParam int pageIndex,
                                  @RequestParam int pageSize) {
        BaseVo result = new BaseVo();
        if (lang == null || pageIndex == 0 || pageSize == 0) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (StringUtils.isBlank(lang)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (companyNumber != null && StringUtils.isBlank(companyNumber)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (appNumber != null && StringUtils.isBlank(appNumber)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (key != null && StringUtils.isBlank(key)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        SelectAppColumn selectAppColumn = articleColumnAppExtensionService.selectArticleColumnAppList(lang, companyNumber, appNumber, key, order, pageIndex, pageSize);
        result.setData(selectAppColumn);
        return result;
    }
    @ResponseBody
    @ApiOperation(value = "唯一性验证", notes = "唯一性验证")
    @RequestMapping(value = "/app/check", method = RequestMethod.GET)
    public BaseVo selectColumnCheck(@RequestParam String lang,
                                    @RequestParam(required = false) String appNumber,
                                    @RequestParam(required = false) String columnId,@RequestParam(required = false) String appColumnId) {
        BaseVo result = new BaseVo();
        if (StringUtils.isBlank(lang)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (appNumber != null && StringUtils.isBlank(appNumber)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (columnId != null && !StringUtils.isNumeric(columnId)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (appColumnId!=null&&!StringUtils.isNumeric(appColumnId)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int count = articleColumnAppExtensionService.checkColumnAppList(lang, appNumber, columnId,appColumnId);
        ArticleColumnCheckVo articleColumnCheckVo = new ArticleColumnCheckVo();
        articleColumnCheckVo.setCheck(2);
        if (count != 0) {
            articleColumnCheckVo.setCheck(1);
            result.setData(articleColumnCheckVo);
        } else {
            result.setData(articleColumnCheckVo);
        }
        return result;
    }
    
}





